/*
 * Copyright (c) 2010 by Chances.
 * $HeadURL$:
 * $Author$:
 * $Date$:
 * $Rev$:
 */
package chances.epg.commons.template;

import java.io.Writer;
import java.util.Map;

import javax.servlet.jsp.PageContext;

import org.apache.velocity.exception.VelocityException;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 模板合成工具类.
 * 
 * @author <a href="mailto:chenxx@chances.com.cn">chenxx</a>
 */
public class TemplateUtils {

	private static TemplateUtils util;

	private TemplateEngine engine;

	public static TemplateUtils getInstance(PageContext context) {
		if (util == null) {
			util = new TemplateUtils();
			util.engine = (TemplateEngine) WebApplicationContextUtils
					.getWebApplicationContext(context.getServletContext())
					.getBean("templateEngine");
		}
		return util;
	}

	/**
	 * 按传输参数按模板进行转换.
	 * @param props
	 * @param vmName
	 * @param writer
	 * @throws VelocityException
	 */
	public void merge(Map<String, Object> props, String vmName,Writer writer) throws VelocityException {
		engine.execute(props, vmName, writer);

	}

	/**
	 * 按传输参数按模板进行转换.
	 * @param obj
	 * @param vmName
	 * @param writer
	 * @throws VelocityException
	 */
	public void merge(Object obj, String vmName, Writer writer)
			throws VelocityException {
		engine.execute(vmName, obj, writer);
	}
}
